﻿Imports System.Data.OleDb
Partial Class tips
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Button1.Visible = False
    End Sub

    Protected Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged
        'Check if user likes the link or not
        If User.Identity.IsAuthenticated Then
            Dim SQL As String = "SELECT * FROM TipLike WHERE TipID = @f1 AND UserId = @f2"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
            cmd.Parameters.AddWithValue("@f1", ListView1.SelectedValue)
            cmd.Parameters.AddWithValue("@f2", Class1.getUserNumber(User.Identity.Name))
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            dr.Read()
            If dr.HasRows Then
                'Do nothing
            Else
                Button1.Visible = True
            End If
            dr.Close()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()

        End If



    End Sub

    Function getlink()
        Dim tipNo As Integer = ListView1.SelectedValue
        Dim link2 As String = "<A href=commentOnTip.aspx?id=" & tipNo & ">Add my Comment</a>"
        Return link2

    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        'Likes the Tip
        'Check User is logged on if not do nothing
        If User.Identity.IsAuthenticated Then
            Dim tn As Integer = ListView1.SelectedValue
            Dim un As Integer = Class1.getUserNumber(User.Identity.Name)
            Dim SQL As String = "INSERT INTO TipLike (TipID,UserId) VALUES (@F1,@F2)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
            cmd.Parameters.AddWithValue("@f1", tn)
            cmd.Parameters.AddWithValue("@f2", un)

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
        End If

    End Sub
End Class